package com.zb.jsp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.zb.jsp.vo.UserRegisterVo;
import com.zb.jsp.vo.UserVo;

public class UserRegisterDao {
	private static String URL = "jdbc:mysql://localhost:3306/ggyx?useUnicode=true&characterEncoding=utf-8";
	private static String USERNAME = "root";
	private static String PWD = "sa";

	/**
	 * 根据用户名查询数据库信息
	 * 
	 * @param userName
	 *            要查找的用户名
	 * @throws SQLException
	 *             数据库链接异常
	 */
	public void userRegister(UserRegisterVo userRegisterVo) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		// 注册mysql驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");

			// 获取连接
			conn = DriverManager.getConnection(URL, USERNAME, PWD);
			// 创建statment对象
			stmt = conn.createStatement();

			// 按用户名查询 sql语句
			String sql = "insert into tbl_register_user "
					+ "(email,passwd,name_cn,sex,age,weight,height,blood_type,"
					+ "belief,degree,industry,income,province,marriage,bak1,bak2) "
					+ "values ('"
					+ userRegisterVo.getEmail()
					+ "','"
					+ userRegisterVo.getPasswd()
					+ "','"
					+ userRegisterVo.getNameCn()
					+ "','"
					+ userRegisterVo.getSex()
					+ "','"
					+ userRegisterVo.getAge()
					+ "','"
					+ userRegisterVo.getWeight()
					+ "','"
					+ userRegisterVo.getHeight()
					+ "','"
					+ userRegisterVo.getBloodType()
					+ "','"
					+ userRegisterVo.getBelief()
					+ "','"
					+ userRegisterVo.getDegree()
					+ "','"
					+ userRegisterVo.getIndustry()
					+ "','"
					+ userRegisterVo.getIncome()
					+ "','"
					+ userRegisterVo.getProvince()
					+ "','"
					+ userRegisterVo.getMarriage()
					+ "','"
					+ userRegisterVo.getBak1()
					+ "','"
					+ userRegisterVo.getBak2() + "');";
			
			System.out.println("sql语句："+sql);
			// 将查询结果返回数据集
			stmt.execute(sql);
			System.out.println("数据库修改成功,修改时间"+new Date());
		} catch (ClassNotFoundException e) {
			// 捕获注册驱动异常
			e.printStackTrace();
		} finally {
			// 倒序关闭连接
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		}

	}
}
